package com.yunkazaixian.syntelecom.utils;

import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    static SimpleDateFormat ymdFormat ;
    static SimpleDateFormat ymdHmsFormat;

    public static SimpleDateFormat getYmdFormat() {
        if (ymdFormat==null){
            ymdFormat = new SimpleDateFormat("yyyyMMdd");
        }
        return ymdFormat;
    }

    public static SimpleDateFormat getYmdHmsFormat() {
        if (ymdHmsFormat==null){
            ymdHmsFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        return ymdHmsFormat;
    }

    public static Date addMonth(Date date, int month) {

        if (date == null) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();//得到Calendar实例
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);//把月份减三个月

        return calendar.getTime();
    }

    public static Date addDay(Date date, int day) {

        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();//得到Calendar实例
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, day);

        return calendar.getTime();
    }

    public static Date strToDate(String source) {
        Date date = null;
        try {
            date = getYmdFormat().parse(source);
        } catch (Exception e) {
        }

        try {
            date = getYmdHmsFormat().parse(source);
        } catch (Exception e) {
        }
        return date;
    }

    public static String dateToStr(Date date) {
        return getYmdFormat().format(date);
    }

    public static String dateToYmdHis(Date date) {
        return getYmdHmsFormat().format(date);
    }

    public static Date min(Date d1, Date d2) {
        if (d1 == null || d2 == null) {
            return null;
        }
        return new Date(Math.min(d1.getTime(), d2.getTime()));
    }

    public static Date max(Date d1, Date d2) {
        if (d1 == null || d2 == null) {
            return null;
        }
        return new Date(Math.max(d1.getTime(), d2.getTime()));
    }


    /***
     *
     * @param date1
     * @param date2
     * @return 先有date2，再有date1，返回1   date1>date2  date1晚
     */
    public static Integer compare(Date date1, Date date2) {
        if (date1 != null && date2 != null) {
            if (date1.getTime() - date2.getTime() > 0) {
                return 1;
            } else if (date1.getTime() - date2.getTime() < 0) {
                return -1;
            } else {
                return 0;
            }
        }
        return null;
    }


    public static int getMonthDiff(Date date1, Date date2) {
        if (date1 != null && date2 != null) {
            Calendar calendar1 = Calendar.getInstance();//得到Calendar实例
            calendar1.setTime(date1);
            int year1 = calendar1.get(Calendar.YEAR);
            int month1 = calendar1.get(Calendar.MONTH);

            Calendar calendar2 = Calendar.getInstance();//得到Calendar实例
            calendar2.setTime(date2);
            int year2 = calendar2.get(Calendar.YEAR);
            int month2 = calendar2.get(Calendar.MONTH);
            return month2 + (year2 - year1) * 12 - month1;
        }
        return 0;
    }

    public static String getFirstDayOfCurrent() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar cal_1 = Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, 0);
        cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        return format.format(cal_1.getTime());
    }

    public static String getFirstDayOfMonth(String now) {

        return now.substring(0, 6) + "01";
    }
    public static String getLastDayOfMonth(String now) {
        int year = Integer.parseInt(now.substring(0, 4));
        int month = Integer.parseInt(now.substring(4, 6));
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month);
        //获取当月最小值
        int lastDay = cal.getMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中的月份，当月+1月-1天=当月最后一天
        cal.set(Calendar.DAY_OF_MONTH, lastDay - 1);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth;
    }


    /**
     * 剩余天数
     * @param arrive_time  到期时间
     * @return
     */
    public static Long getRemainingDays(String arrive_time) {
        if (!StringUtils.hasText(arrive_time)) {
            return null;
        }


        long arr = strToDate(arrive_time).getTime();
        long now = strToDate(dateToStr(new Date())).getTime();

        long l = arr - now;

        return l/1000/60/60/24;

    }


    public static boolean isDangYue(String inputDate){
        String today= DateUtils.dateToStr(new Date());
        String lastDay= DateUtils.getLastDayOfMonth(today);
        String firstDay= DateUtils.getFirstDayOfCurrent();

        if (DateUtils.compare(DateUtils.strToDate(firstDay),DateUtils.strToDate(inputDate))<=0&&DateUtils.compare(DateUtils.strToDate(inputDate),DateUtils.strToDate(lastDay))<=0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * main
     */
    public static void main(String[] args) {
        System.out.println(getRemainingDays("20230810"));

    }
}
